package com.example.backspring;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;

import java.net.InetAddress;
import java.net.UnknownHostException;

@MapperScan("com.example.backspring.dao*")
@SpringBootApplication(scanBasePackages = "com.example.backspring")
public class BackSpringApplication {
    public static void main(String[] args) {
        SpringApplication.run(BackSpringApplication.class, args);
    }

    @Bean
    public ApplicationListener<WebServerInitializedEvent> onWebServerReady() {
        return event -> {
            int port = event.getWebServer().getPort();
            String host = getHostname();
//            System.out.println("前端启动了吗？前端主页 URL: http://" + host + ":" + port + "/");
//            System.out.println("OpenAPI URL: http://" + host + ":" + port + "/v3/api-docs");

            System.out.println("\n**************************************");
            System.out.println("Swagger2.0 UI URL: http://" + host + ":" + port + "/doc.html");
            System.out.println("**************************************\n");
        };
    }

    private String getHostname() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            return "localhost";
        }
    }
}
